<?php
namespace Client\Controller;

use Org\Net\Http;

use Think\Image;

use Think\Controller;

header("Content-type: text/html; charset=utf-8");

class PublishController extends Controller{
	public function getLBSPublish(){
		$http = new Http();
		$geotable_id ='105582';
		$url = C('BAIDU_POI_LIST')."?ak=".C('BAIDU_AK')."&geotable_id={$geotable_id}&page_size=10";
		$result = $http->fsockopenDownload($url);
		var_dump(json_decode($result,'json'));
	}
	//添加约吧
	public function savePublish(){

		if(IS_POST){
			$status = M('account')->where(array('id'=>I('uid')))->getField('status');
			if($status==1){
				$http = new Http();
				$post['latitude'] = $_POST['latitude'];
				$post['longitude'] = $_POST['longitude'];
				$post['geotable_id'] = '105582';
				$post['ak'] = C("BAIDU_AK");
				$post['coord_type'] = 3;
				$post['address'] = $_POST['address'];
				$post['title'] = $_POST['content'];
				$post['tags'] = $_POST['tag'];
				$data['post'] = $post;
				$back = $http->fsockopenDownload(C("BAIDU_POI_CREATE"),$data);
				//			file_put_contents("Public/Log/log.txt", "back:".$back."\r\n",FILE_APPEND|LOCK_EX);
				$back = json_decode($back);
					
				if($back->status == 0){
					$files = $_FILES;
					$time = time();
					$publish_info['address']=$_POST['address'];
					$publish_info['content']=I('content');
					$publish_info['time'] = I('time');
					$publish_info['ctime']=time();
					$publish_info['uid'] = I('uid');
					$publish_info['tag'] =I('tag');
					$publish_info['related_destination']= I('relatedDestination');
					$publish_info['poi_id'] = $back->id;
					$image_url = '';
					$data["code"] = 0;
					foreach ($files as $file){
						$uploadpath = "Public/uploads/Yue/";//上传路径

						$util = A('Util');
						$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建

						$pinfo = pathinfo($file['name']);
						$ftype = $pinfo['extension'];//文件类型
						$name = basename($file['name'],".{$ftype}");

						$fileName = md5($name.time())."_{$data["code"]}.".$ftype;//识别图
						$image = new Image();
						$image->open($file['tmp_name']);
						if($file['size']<1048576){
							$image->save($uploadpath.$fileName);
						}
						$data["image"][] = $uploadpath.$fileName;
						$image_url .= "http://qianmosen.cn/tubu/".$uploadpath.$fileName.",";
						$data["code"]++;
					}
					$publish_info['image'] = substr($image_url, 0,strlen($image_url)-1);
					$publish = D("Publish");
					if($publish->create($publish_info)){
						$publish->add();
						$json['result'] = "0";
						$json['msg'] = '发布成功';
					}else{
						$json['result'] = "-1";
						$json['msg'] = $publish->getError();
					}
				}else{
					$json['result'] = "-1";
					$json['msg'] = $back->message;
				}
			}else{
				$json['result'] = "-2";
				$json['msg'] = '用户信息未完善';
			}
			$this->ajaxReturn($json);
		}
	}

	public function getNearby(){
		$http = new Http();
		$publish = D('PublishView');
		$location = I('location');
		$geotable_id ='105582';
		$url = C('BAIDU_GEOSEARCH')."?ak=".C('BAIDU_AK')."&sortby=distance:1&geotable_id={$geotable_id}&location={$location}&radius=3000&page_size=10";
		$result = $http->fsockopenDownload($url);
		$res = json_decode($result,'json');
// 		var_dump($res);
		$where = array();
		if($res['size']>0){
			foreach($res['contents'] as $obj){
				$poi_id = $obj['uid'];
				array_push($where,$poi_id);
			}
			$w['poi_id']=array("in",$where);
			$resu=$publish->where($w)->select();
			if(sizeof($resu)>0){
				$json['result']="0";
				$json['msg']="获取成功";
				$json['publish']=$resu;
			}
			else{
				$json['result'] = "-1";
				$json['msg'] = $publish->getError();
			}
		}
		else{
			$json['result'] = "-2";
			$json['msg'] ='无法检索到附近的用户';
		}

		$this->ajaxReturn($json);
	}

	//下拉刷新约吧，获取约吧的最新前十条信息
	public function reflashPublish(){
		S('result',null);
		$publish = D('PublishView');
		$result  = $publish->limit(0,10)->order(array('ctime'=>'desc'))->select();
		if(sizeof($result)>0){
			$json['result']="0";
			$json['msg']="获取成功";
			foreach ($result as $k=>$v){
				$result[$k]['ctime'] = date('Y-m-d H:i:s',$v['ctime']);
			}
			$json['publish']=$result;
			$this->ajaxReturn($json);
		}
		else{
			$this->ajaxReturn(array('msg'=>'没有更多的信息了','result'=>'-1'),'json');
		}
	}

	public function searchPublish(){
		if(IS_POST){
			$search = $_POST['searchContent'];
			$publish = D('PublishView');
			$result = $publish->where("content regexp '".$search."' or Publish.address regexp '".$search."' or tag regexp '".$search."'")->order(array('ctime'=>'desc'))->select();
			$scenic = M('Scenic')->where("name regexp '".$search."' or address regexp '".$search."' or intro regexp '".$search."' or type regexp '".$search."'")->select();
			$where['name'] = array('like',"%{$search}%");
			$where['type'] = 0;
			$tickets = D('ScenicTicket');
			$ticket = $tickets->relation(true)->where($where)->select();
			$where2['name'] = array('like',"%{$search}%");
			$where2['type'] = 1;
			$entertainment = $tickets->relation(true)->where($where2)->select();
			//			$entertainment = M('ScenicTicket')->where("name regexp '".$search."'")->select();
			//			$entertainment = array();
			if(sizeof($result)>0||sizeof($scenic)>0||sizeof($ticket)>0||sizeof($entertainment)>0){
				foreach ($result as $k=>$v){
					$result[$k]['ctime'] = date('Y-m-d H:i:s',$v['ctime']);
				}
				$ticketres = array();
				foreach ($ticket as $k=>$v){
					$ticketres[$k]['ticket_time'] = date('Y-m-d H:i:s',$v['time']);
					$ticketres[$k]['ticket_id']   = $v['id'];
					$ticketres[$k]['ticket_name'] = $v['name'];
					$ticketres[$k]['ticket_price'] = $v['price'];
					$ticketres[$k]['ticket_deadline'] = $v['deadline'];
					$ticketres[$k]['ticket_original'] = $v['original'];
					$ticketres[$k]['ticket_image'] = $v['image'];
					$ticketres[$k]['scenic_image'] = $v['scenic_image'];
					$ticketres[$k]['scenic_id']=$v['scenic_id'];
					$ticketres[$k]['scenic_name']=$v['scenic_name'];
					$ticketres[$k]['scenic_address']=$v['scenic_address'];
					$ticketres[$k]['scenic_latitude']=$v['scenic_latitude'];
					$ticketres[$k]['scenic_longitude']=$v['scenic_longitude'];
					$ticketres[$k]['scenic_phone'] = $v['scenic_phone'];
				}
				$entertainmentres = array();
				foreach ($entertainment as $k=>$v){
					$entertainmentres[$k]['ticket_time'] = date('Y-m-d H:i:s',$v['time']);
					$entertainmentres[$k]['ticket_id']   = $v['id'];
					$entertainmentres[$k]['ticket_name'] = $v['name'];
					$entertainmentres[$k]['ticket_price'] = $v['price'];
					$entertainmentres[$k]['ticket_deadline'] = $v['deadline'];
					$entertainmentres[$k]['ticket_original'] = $v['original'];
					$entertainmentres[$k]['ticket_image'] = $v['image'];
					$entertainmentres[$k]['scenic_image'] = $v['scenic_image'];
					$entertainmentres[$k]['scenic_id']=$v['scenic_id'];
					$entertainmentres[$k]['scenic_name']=$v['scenic_name'];
					$entertainmentres[$k]['scenic_address']=$v['scenic_address'];
					$entertainmentres[$k]['scenic_latitude']=$v['scenic_latitude'];
					$entertainmentres[$k]['scenic_longitude']=$v['scenic_longitude'];
					$entertainmentres[$k]['scenic_phone'] = $v['scenic_phone'];
				}
				$json['result']="0";
				$json['msg']="获取成功";
				$json['publish'] = $result;
				$json['scenic'] = $scenic;
				$json['ticket'] = $ticketres;
				$json['entertainment'] =  $entertainmentres;
				$this->ajaxReturn($json);
			}else{
				$this->ajaxReturn(array('msg'=>'没有更多的信息了','result'=>'-1'),'json');
			}
		}
		else {
			$this->ajaxReturn(array('msg'=>'失败，数据不是以POST方式提交','result'=>'-2'),'json');
		}
	}

	//加载更多约吧信息，加了缓存，每次加载十条
	public function getPublish(){
		$page_num = I('page_num');
		$publish  = D('PublishView');
		if($page_num!=null||$page_num!=''){
			//			file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
			$start = $page_num*10;
			$end   = $page_num+10;
			if(!$result = S('result')){
				$result  = $publish->order(array('ctime'=>'desc'))->select();
				S('result',$result,600);
			}
			$json['result']="0";
			$json['msg']="获取成功";
			$res = array_slice($result,$start,10);
			foreach ($res as $k=>$v){
				$res[$k]['ctime'] = date('Y-m-d H:i:s',$v['ctime']);
			}
			$json['publish']=$res;

			if(sizeof($json['publish'])>0){
				$this->ajaxReturn($json);
			}
			else{
				$this->ajaxReturn(array('msg'=>'没有更多的信息了','result'=>'-1'),'json');
			}
		}
		else{
			$this->ajaxReturn(array('msg'=>'失败，没有传递正确的标签','result'=>'-2'),'json');
		}

	}
	public function getByTag(){
		if(IS_POST){
			$page_num = I('page_num');
			if($page_num==0){
				S('tresult',null);
			}
			$publish  = D('PublishView');
			$tag = I("tag");
			//if(!$tresult = S('tresult')){
			$tresult  = $publish->where(array('tag'=>$tag))->order(array('ctime'=>'desc'))->select();
			//	S('tresult',$tresult,600);
			//}
			$start = $page_num*10;
			if(sizeof($tresult)>0){
				$res = array_slice($tresult,$start,10);
				foreach ($res as $k=>$v){
					$res[$k]['ctime'] = date('Y-m-d H:i:s',$v['ctime']);
				}
				$json['result']="0";
				$json['msg']="获取成功";
				$json['publish']=$res;
				$this->ajaxReturn($json);
			}
			else{
				$this->ajaxReturn(array('msg'=>'没有更多的信息了','result'=>'-1'),'json');
			}
		}
		else{
			$this->ajaxReturn(array('msg'=>'失败，没有传递正确的标签','result'=>'-2'),'json');
		}
	}
	public function myPublish(){
			
		$publish  = D('PublishView');
		if(IS_POST){
			$uid  	  = $_POST['id'];
			$result   = $publish->where(array('uid'=>$uid))->order(array('ctime'=>'desc'))->select();
			foreach ($result as $k=>$v){
				$result[$k]['ctime'] = date('Y-m-d H:i:s',$v['ctime']);
			}
			if(sizeof($result)>0){
				$json['result']="0";
				$json['msg']="获取成功";
				$json['publish']=$result;
				$this->ajaxReturn($json);
			}
			else{
				$this->ajaxReturn(array('msg'=>'没有更多的信息了','result'=>'-1'),'json');
			}
		}
		else{
			$this->ajaxReturn(array('msg'=>'失败，数据不是以POST方式提交','result'=>'-2'),'json');
		}
	}
	public function getByID(){
		$publish  = D('PublishView');
		if(IS_POST){
			$pid  	  = $_POST['pid'];
			$result   = $publish->where(array('id'=>$pid))->find();
			$result['ctime'] = date('Y-m-d H:i:s',$v['ctime']);
			if(sizeof($result)>0){
				$json['result']="0";
				$json['msg']="获取成功";
				$json['publish']=$result;
				$this->ajaxReturn($json);
			}
			else{
				$this->ajaxReturn(array('msg'=>'没有更多的信息了','result'=>'-1'),'json');
			}
		}
		else{
			$this->ajaxReturn(array('msg'=>'失败，数据不是以POST方式提交','result'=>'-2'),'json');
		}
	}
	public function downloads(){
		$name_tmp = explode("_",'tubu-1.0.0.apk');
		$type = $name_tmp[0];
		$file_time = explode(".",$name_tmp[3]);
		$file_time = $file_time[0];
		$file_date = date("Y/md",$file_time);
		$downloadpath = "Public/download/";
		$file_dir = $downloadpath;
			
		if (!file_exists($file_dir.'tubu-1.0.0.apk')){
			header("Content-type: text/html; charset=utf-8");
			echo "File not found!";
			exit;
		} else {
			$file = fopen($file_dir.'tubu-1.0.0.apk',"r");
			Header("Content-type: application/octet-stream");
			Header("Accept-Ranges: bytes");
			Header("Accept-Length: ".filesize($file_dir . 'tubu-1.0.0.apk'));
			Header("Content-Disposition: attachment; filename=".'tubu-1.0.0.apk');
			echo fread($file, filesize($file_dir.'tubu-1.0.0.apk'));
			fclose($file);
		}
	}


}